package cn.wonderland.system.controller;

import cn.wonderland.base.util.AjaxResult;
import cn.wonderland.base.util.PageList;
import cn.wonderland.system.domain.SDType;
import cn.wonderland.system.query.SDTypeQuery;
import cn.wonderland.system.service.SDTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/type")
@Api(value = "字典类型的API",description="字典类型相关的CRUD功能")
public class SDTypeController {
    @Autowired
    private SDTypeService sdTypeService;

    @GetMapping
    public List<SDType> findAll(){
        return sdTypeService.findAll();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public SDType findById(@ApiParam(value="ID属性",required = true) @PathVariable("id") Long id){
        return sdTypeService.findById(id);
    }

    @PutMapping()
    public AjaxResult addOrUpdate(@RequestBody SDType sDType){
        if(sDType.getId() != null){ // id不为空，执行修改
            sdTypeService.update(sDType);
        }else{ // id为空，执行新增
            sdTypeService.add(sDType);
        }
        return AjaxResult.me().success();
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        sdTypeService.delete(id);
        return AjaxResult.me().success();
    }

    @PostMapping()
    public PageList<SDType> queryByPage(@RequestBody SDTypeQuery query){
        return sdTypeService.queryByPage(query);
    }

    // 批量删除
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        sdTypeService.batchDelete(ids);
        return AjaxResult.me().success();
    }
}
